﻿using National.Bll.IService;
using National.Dal;
using National.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace National.Bll
{
    public class BaseService<T>:IDenpendecy ,IBaseService<T>where T : class
    {
        //当前操作的数据库实例
        private BaseRepository<T, ShopVueEntities> db = new BaseRepository<T, ShopVueEntities>();
        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  bool Add(T entity, int role, int module)
        {
            return db.Add(entity);
        }
        //添加多条数据
        public  bool AddRange(List<T> entity)
        {

            return db.AddRange(entity);


        }
        #endregion
        #region 删除
        /// <summary>
        /// 删除单条
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  bool Delete(T entity)
        {
            return db.Delete(entity);

        }
        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public  bool BatchDelete(List<T> entitys)
        {
            return db.BatchDelete(entitys);

        }
        /// <summary>
        /// 根据ID删除多条
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public  bool BatchDelete(params int[] ids)
        {
            return db.BatchDelete(ids);

        }
        /// <summary>
        /// 删除单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public  bool Delete(int id)
        {
            return db.Delete(id);
        }
        #endregion
        #region 修改
        //修改
        public  bool Update(T entity)
        {
            return db.Update(entity);
        }
        #endregion
        #region 查询
        /// <summary>
        /// 返回一个集合
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public  List<T> QueryList(Expression<Func<T, bool>> lambdaExpression)
        {
            return db.QueryList(lambdaExpression);

        }
        /// <summary>
        /// 返回一个对象，一般根据主健不能查出两个,如果不存在返回默认值
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public  T Query(Expression<Func<T, bool>> lambdaExpression)
        {
            return db.Query(lambdaExpression);
        }
        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public  bool Exists(Expression<Func<T, bool>> lambdaExpression)
        {
            return db.Exists(lambdaExpression);
        }
        #endregion
        #region 分页
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderbyLambda"></param>
        /// <param name="count"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public List<T> QueryPageList<S>(int PageSize, int PageIndex, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, out int count, bool isAsc = true)
        {
            return db.QueryPageList(PageSize, PageIndex, whereLambda, orderbyLambda, out count, isAsc);

        }
        #endregion
    }
}
